﻿namespace DotNetUtility.GenericDesignPattern
{
    using System;

    /// <summary>
    /// To do ternary operation. 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <example>
    ///     <code>
    ///         public class TernaryOperationPatternTest
    ///         {
    ///             public Action UseTernaryOperationPattern()
    ///             {
    ///                 return
    ///                     TernaryOperation<Action>.Operate(() => { return true; }, 
    ///                     TernaryOperation_Action_Test_Method_True, 
    ///                     TernaryOperation_Action_Test_Method_False);
    ///             }
    ///             
    ///             private void TernaryOperation_Action_Test_Method_True()
    ///             {
    ///                 Console.WriteLine("TernaryOperation_Action_Test_Method_True");
    ///             }
    ///
    ///             private void TernaryOperation_Action_Test_Method_False()
    ///             {
    ///                 Console.WriteLine("TernaryOperation_Action_Test_Method_False");
    ///             }
    ///         }
    ///     </code>
    /// </example>
    public static class TernaryOperation<T>
    {
        /// <summary>
        /// To execute the ternary operation.
        /// </summary>
        /// <param name="condition">Selection condition</param>
        /// <param name="trueValue">Return value when the Selection Condition will be true</param>
        /// <param name="falseValue">Return value when the Selection Condition will be false</param>
        /// <returns>T</returns>
        /// <example>
        ///     <code>
        ///         Action result= TernaryOperation<Action>.Operate(() => { return true; }, TernaryOperation_Action_Test_Method_True, TernaryOperation_Action_Test_Method_False);
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static T Operate(Func<bool> condition, T trueValue, T falseValue)
        {
            return condition() ? trueValue : falseValue;
        }

        public static T Operate(bool condition, T trueValue, T falseValue)
        {
            return condition ? trueValue : falseValue;
        }

    }
}
